<template>
  <div>
    <el-form-item label="图片适应模式">
      <el-select v-model="activeData.config['imageFit']" placeholder="请选择">
        <el-option label="填充" value="fill" />
        <el-option label="包含" value="contain" />
        <el-option label="覆盖" value="cover" />
        <el-option label="原始" value="none" />
        <el-option label="缩放" value="scale-down" />
      </el-select>
    </el-form-item>
    <el-form-item label="高度">
      <el-input-number v-model="activeData.config['height']" label="高度" :min="50" :max="10000" :step="5" />
    </el-form-item>
    <el-divider>选项</el-divider>
    <draggable :animation="340" :list="activeData.config.options" group="selectItem" handle=".option-drag">
      <div v-for="(item, index) in activeData.config.options" :key="index" class="select-item">
        <div class="select-line-icon option-drag">
          <i class="el-icon-s-operation" />
        </div>
        <div class="width-full">
          <div class="flex-row">
            <el-input v-model="item.label" placeholder="选项名" size="small" />
            <div class="close-btn select-line-icon" @click="activeData.config.options.splice(index, 1)">
              <i class="el-icon-remove-outline" />
            </div>
          </div>
          <div class="flex-row">
            <el-input v-model="item.image" placeholder="图片" size="small" />
            <el-upload
              ref="logoUpload"
              :action="getUploadUrl"
              :headers="getUploadHeader"
              :on-progress="uploadProgressHandle"
              :on-success="
                (response, file, fileList) => {
                  item.image = response.data
                  $set(activeData.config.options, index, item)
                  closeUploadProgressHandle()
                }
              "
              :show-file-list="false"
              accept=".jpg,.jpeg,.png,.gif,.bmp,.JPG,.JPEG,.PBG,.GIF,.BMP"
              style="text-align: center"
            >
              <div slot="trigger" class="select-line-icon">
                <i class="el-icon-upload" />
              </div>
            </el-upload>
          </div>
        </div>
      </div>
    </draggable>
    <div style="margin-left: 20px">
      <el-button icon="el-icon-circle-plus-outline" style="padding-bottom: 0" type="text" @click="addImageCarouselItem">
        添加选项
      </el-button>
    </div>
    <el-divider />
  </div>
</template>

<script>
import mixin from './mixin'
import _ from 'lodash'
import draggable from 'vuedraggable'

export default {
  name: 'ConfigItemImageCarousel',
  components: {
    draggable
  },
  mixins: [mixin],
  methods: {
    addImageCarouselItem() {
      const lastItem = _.last(this.activeData.config.options)
      this.activeData.config.options.push({
        label: '',
        image: '',
        value: lastItem ? lastItem.value + 1 : 1
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.select-item {
  display: flex;
  border: 1px dashed #fff;
  box-sizing: border-box;
  align-items: center;

  & .close-btn {
    cursor: pointer;
    color: #f56c6c;
  }

  & .el-input + .el-input {
    margin-left: 4px;
  }
}

.select-item + .select-item {
  margin-top: 4px;
}

.select-item.sortable-chosen {
  border: 1px dashed #409eff;
}

.select-line-icon {
  line-height: 32px;
  font-size: 22px;
  padding: 0 4px;
  color: #777;
}

.option-drag {
  cursor: move;
}
</style>
